using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class InfoPathBaseActivity : Activity
    {
        protected IPAccessHelper _ipHelper;

        protected bool AutoLoadForm = true;
        protected bool AutoSaveForm;

        #region  Base bound properties

        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (InfoPathBaseActivity));

        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(ResetListItemPermissionInheritance.__ContextProperty))); }
            set { base.SetValue(ResetListItemPermissionInheritance.__ContextProperty, value); }
        }

        #endregion

        public InfoPathBaseActivity()
        {
            InitializeComponent();
        }

        public InfoPathBaseActivity(string name) : base(name)
        {
            //Called via base(name)
            //InitializeComponent();
        }


        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            _ipHelper = new IPAccessHelper(__Context);

            if (AutoLoadForm)
            {
                try
                {
                    _ipHelper.LoadForm();
                }
                catch (Exception e)
                {
                    Exception we = Common.WrapWithFriedlyException(e,
                                                                   "Error loading form data file. Likely causes: this file is not a valid InfoPath Form!");

                    Common.LogExceptionToWorkflowHistory(we, executionContext, WorkflowInstanceId);

                    throw we;
                }
            }

            ExecuteIPActivity(executionContext);

            if (AutoSaveForm)
            {
                try
                {
                    _ipHelper.SaveForm();
                }
                catch (Exception e)
                {
                    Exception we = Common.WrapWithFriedlyException(e, "Error saving form");

                    Common.LogExceptionToWorkflowHistory(we, executionContext, WorkflowInstanceId);

                    throw we;
                }
            }

            _ipHelper.Dispose();

            return ActivityExecutionStatus.Closed;
        }

        protected virtual ActivityExecutionStatus ExecuteIPActivity(ActivityExecutionContext executionContext)
        {
            return ActivityExecutionStatus.Closed;
        }
    }
}